<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.ProjectDAO"%>
<%@page import="java.io.FileOutputStream" %>  
<%@page import="java.io.OutputStream" %>  
<%@page import="jxl.Workbook" %>  
<%@page import="jxl.format.Alignment" %> 
<%@page import="jxl.format.Border" %> 
<%@page import="jxl.format.BorderLineStyle" %>  
<%@page import="jxl.format.Colour" %>  
<%@page import="jxl.format.VerticalAlignment" %>  
<%@page import="jxl.write.Label" %>  
<%@page import="jxl.write.WritableCellFormat" %>  
<%@page import="jxl.write.WritableFont" %>  
<%@page import="jxl.write.WritableSheet" %>
<%@page import="jxl.write.WritableWorkbook" %> 
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	ProjectDAO projectDao = (ProjectDAO)ctx.getBean("ProjectDAO");
	UserDAO userDao = (UserDAO)ctx.getBean("UserDAO");
	
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	String beginDate = request.getParameter("beginDate");
	String endDate = request.getParameter("endDate");
	
	int pn = 0;
	int pageSize = 0;
	Integer uid = (Integer)session.getAttribute("id");
	Integer teamId = (Integer)session.getAttribute("tid");
	
	User user = userDao.findById(uid);
	request.setAttribute("user",user);
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "isDelete", 0});
	where.add(new Object[]{"eq", "teamId", teamId});
	
	if(searchType != null && !"".equals(searchType)){
		if("1".equals(searchType))
			where.add(new Object[]{"like", "m.code", searchKeyword});
		else if("2".equals(searchType))
			where.add(new Object[]{"like", "m.name", searchKeyword});
	}
	
	if(beginDate != null && !"".equals(beginDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
		where.add(new Object[]{"ge", "date", dateVal});
	}
	
	if(endDate != null && !"".equals(endDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
		where.add(new Object[]{"le", "date", dateVal});
	}
	
	if(searchKeyword != null && !"".equals(searchKeyword)){
		Map map = receivepurmatDao.getReceivepurmat(pn, pageSize,
				new String[][]{new String[]{"desc","date"}},
				where,
				new String[][]{new String[]{"","id"},new String[]{"","p.name"},new String[]{"","r.supplier"},new String[]{"","m.code"},new String[]{"","m.name"},new String[]{"","m.company"},new String[]{"","purPrice"},new String[]{"","purNum"}}		
				,new String[]{"receiveorder","place","material","substore","project"});
		
		request.setAttribute("map",map);
		
		int pageTotal = ((Integer)map.get("pageTotal")).intValue();
		int pageNo = ((Integer)map.get("pageNo")).intValue();
		List pageList=getPage(pageTotal,pageNo);
		request.setAttribute("pages",pageList);
		
		///导出execl
		String sServerFileName="";
		String sLocalFileName = "";
		java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sServerFileName= "exportCheck"+fmt.format(dt);
		 String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
		String sPath=sWebRootPath+"exportTemp/";
		System.out.println("targetfile==============================================================》"+sPath);
		request.setAttribute("xlsName",sServerFileName+".xls");
		String execlName = sPath+sServerFileName+".xls";
		//sServerFileName =sServerFileName + sExt;
		//如果不存在该目录，则新建一个
		//File dir =new File(sPath);
		File dir =new File(sPath);
		if (!dir.exists()){
			dir.mkdirs();
		}
		String targetfile = execlName;// 输出的excel文件名  
        String worksheet = "check_"+sServerFileName;// 输出的excel文件工作表名
        String[] title = { "行号", "库位", "供应商", "物资编码", "物资名称", "单位", "单价", "库存数量","盘点数量" };// excel工作表的标题
       	WritableWorkbook workbook;
       
       	 try {  
            //System.out.println("begin");  
  
            OutputStream os = new FileOutputStream(targetfile);  
            workbook = Workbook.createWorkbook(os);  
            WritableSheet sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表   
            //设置内容格式  
            WritableCellFormat contentFormat = new WritableCellFormat();  
            contentFormat.setAlignment(Alignment.CENTRE);  
            Label label;  
            for (int i = 0; i < title.length; i++) {  
                // Label(列号,行号 ,内容 )  
                label = new jxl.write.Label(i, 0, title[i]); // put the title in  
                                                                // row1  
                label.setCellFormat(contentFormat);  
                sheet.addCell(label);  
            }
            if(map!=null){
            	List list  = (List)map.get("data");
            		System.out.println("targetfile==============================================================》"+list.size());
            	if(list.size()>0){
            		for(int j=0;j<list.size();j++){
            			Object[] ex = (Object[])list.get(j);
            			//label = new jxl.write.Label(j, 0, ""+j);
            			System.out.println("targetfile==============================================================》"+ex.length);
            			for(int k=0;k<ex.length;k++){
            				if(k==0){
            					label = new jxl.write.Label(k, j+1,j+1+"");
            					label.setCellFormat(contentFormat);  
                				sheet.addCell(label);
            				}else{
            					label = new jxl.write.Label(k, j+1,ex[k].toString());
            					label.setCellFormat(contentFormat);  
                				sheet.addCell(label);
            				}
            				
            			}
            		}	
            	}
            }
            workbook.write();  
            workbook.close();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
		
	}
	
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);
	request.setAttribute("beginDate",beginDate);
	request.setAttribute("endDate",endDate);
	//request.setAttribute("endDate",endDate);

	pageContext.forward("/vm/statistics/report_2.vm");
%>
<%!
	
%>